package com.example.sj12;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.TimeZone;

public class Comment extends Activity implements View.OnClickListener {
    private Button btnSubmit;
    private EditText etComment;
    private ListView list,list1;//列表视图对象
    private List<Data> mData = null;//定义数据列表
    private Context mContext;//设备上下文
    private RatingBar rb;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);//安卓进入页面避免软键盘弹出的解决办法
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);//使edittext弹出的软键盘位于输入框下面而不是覆盖输入框
        setContentView(R.layout.activity_comment);
        mContext=this;
        rb=findViewById(R.id.rb);
        //点击空白处让Activity消失，可在Style中设置
//        setFinishOnTouchOutside(true);
        Bundle b=getIntent().getExtras();
        //获取Bundle的信息
        final String househost=b.getString("name");
        final String housename=b.getString("housename");
        final String username=b.getString("username");
        final String url=b.getString("url");
        etComment = (EditText) findViewById(R.id.et_comment);
        list1=findViewById(R.id.list);
        new Thread() {
            @Override
            public void run() {
                super.run();
                // 耗时操作
                UserDao d = new UserDao();
                mData = new LinkedList<>();//创建数据链表
                mData = d.getComment(housename);//给数据链表增加内容
                //Log.i(TAG,"fun");
                //====================
                Handler mHandler = new Handler(Looper.getMainLooper()) {

                    @Override
                    public void handleMessage(@NonNull Message msg) {
                        super.handleMessage(msg);
                        // 切换到主线程
                        //一开始显示
                        list1.setVisibility(View.VISIBLE);
                        DataAdapterComment mAdapter = new DataAdapterComment((LinkedList<Data>) mData, mContext);//初始化适配器对象
                        list1.setAdapter(mAdapter);//设置适配器
                    }
                };
                mHandler.sendEmptyMessage(0);

            }
        }.start();
        new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
//                InputMethodManager inputMethodManager=(InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
//                inputMethodManager.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
                InputMethodManager inputManager =
                        (InputMethodManager) etComment.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputManager.showSoftInput(etComment, 0);
                return false;
            }
        }).sendEmptyMessageDelayed(0, 300);
        btnSubmit = (Button) findViewById(R.id.btn_submit);
        btnSubmit.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_submit:
                Bundle b=getIntent().getExtras();
                //获取Bundle的信息
                final String househost=b.getString("name");
                final String housename=b.getString("housename");
                final String username=b.getString("username");
                final String url=b.getString("url");
                final float rating = rb.getRating();
                //final float rating = rb.getNumStars();
                final TimeZone zone = TimeZone.getTimeZone("GMT+8"); //获取中国时区
                TimeZone.setDefault(zone); //设置时区
                final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");// HH:mm:ss
//获取当前时间
                final Date date = new Date(System.currentTimeMillis());
                Toast.makeText(this, etComment.getText().toString(), Toast.LENGTH_SHORT).show();
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        //float rating = rb.getRating();
                        System.out.println("星星："+rating);
                        String comment=etComment.getText().toString().trim();
                        UserDao ud = new UserDao();
                        UserDao u= new UserDao();
                        Boolean res=u.checkinsert(housename,username);
                        if (res) {
                            Looper.prepare();
                            ud.comment(housename,househost,username,comment,simpleDateFormat.format(date),url,rating);
                            Looper.loop();
                        } else {
                            Looper.prepare();
                            Toast toast = Toast.makeText(getApplicationContext(), "尚未入住", Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }
                        //boolean result =ud.register(n,psw);
                        //ud.comment(housename,househost,username,comment,simpleDateFormat.format(date));


                        /*
                        if (ud.check(n,btc.isChecked())) {
                            Looper.prepare();
                            Toast.makeText(getApplicationContext(), "该用户名已被注册，注册失败", Toast.LENGTH_SHORT).show();
                            Looper.loop();
                        } else {

                            //UserDao u = new UserDao();
                            //boolean t=u.register(n, psw);
                            if (!ud.register(n, psw,btc.isChecked())) {
                                Looper.prepare();
                                Toast.makeText(getApplicationContext(), "插入数据表成功", Toast.LENGTH_SHORT).show();
                                Looper.loop();
                            }
                        /*if (!result){
                            Looper.prepare();
                            Toast toast = Toast.makeText(RegisterActivity.this,"注册成功！",Toast.LENGTH_SHORT);
                            toast.show();
                            Looper.loop();
                        }
                            //以上为jdbc注册
                        }*/
                    }
                }).start();
                finish();
                break;
        }
    }
}